define(function(require){
    var main = require('/static/main.js');
    require('/static/ajaxfileupload.js');
    require('/static/area/ProJson.js');
    require('/static/area/DistrictJson.js');
    require('/static/area/CityJson.js');
    var init = require('/static/area/init.js');
    var a = new init();
    var m = new main();
    var add = {
        CaseId :$_GET['caseid'],
        _init:function(){
            this.bindEvent();
            this.upload();
            if(this.CaseId){
                this.getInfo();
                $(".search_litile_tit").html('<span class="tit_diswidth"></span>编辑客户案例');
                $(".q-tit-change span").html('编辑客户案例');
            }
        },
        getInfo:function(){
            var _this = this;
            $.ajax({
                type: "POST",
                data: {CaseId:_this.CaseId},
                url: "/website/case-detail",
                dataType: "json",
                success: function (ret) {
                    var data = ret.Model;
                    _this.setData('Src', data.Src);
                    _this.setData('Title', data.Title);
                    _this.setData('CompanyName', data.CompanyName);
                    _this.setData('CompanyType', data.CompanyType);
                    _this.setData('FansCnt', data.FansCnt);
                    _this.setData('Description', data.Description);
                }
            });
        },
        setData:function(name,data){
            $("."+name).val(data);
        },
        bindEvent:function(){
            var _this = this;
            $(".btn-info").click(function(){
                var JsonData = {
                    Src:$("#AddEnt .Src").val().trim(),
                    Title:$("#AddEnt .Title").val().trim(),
                    CompanyName:$("#AddEnt .CompanyName").val().trim(),
                    CompanyType:$("#AddEnt .CompanyType").val().trim(),
                    FansCnt:$("#AddEnt .FansCnt").val().trim(),
                    Description:$("#AddEnt .Description").val().trim(),
                }
                //console.log(JsonData);
                if(!_this.seterrorMessage(JsonData.Src,'请上传主图') ||
                    !_this.seterrorMessage(JsonData.Title,'请输入标题') ||
                    !_this.seterrorMessage(JsonData.CompanyName,'请输入公司名称') ||
                    !_this.seterrorMessage(JsonData.CompanyType,'请选择企业类型') ||
                    !_this.seterrorMessage(JsonData.FansCnt,'请输入粉丝数') ||
                    !_this.seterrorMessage(JsonData.Description,'请输入描述信息')
                ){
                    return false;
                }

                if( _this.CaseId){
                    JsonData.CaseId = _this.CaseId;
                }
                $.ajax({
                    type: "POST",
                    data: JsonData,
                    url: "/website/case-create",
                    dataType: "json",
                    success: function (ret) {
                        if (ret.IsBizSuccess) {
                            if( _this.CaseId){
                                layer.msg('修改成功');
                            }else{
                                layer.msg('添加成功');
                            }
                            setTimeout(function(){
                                window.location.href = "/website/case"
                            },1000)
                        } else {
                            layer.msg(ret.ErrMsg);
                        }
                        layer.close();
                    }
                })
            });
        },

        upload:function(){
            $(document).on("click", ".fileupload", function(){
                $("#file").click();
            });
            //图片上传
            $("#file").on("change",function () {
                ajaxFileUpload();
            });
            function ajaxFileUpload(){
                $.ajaxFileUpload({
                    url:m.baseUrl+"/customer/edit-head",
                    secureuri: false,
                    fileElementId: 'file',
                    dataType: 'json',
                    success: function (ret) {
                        if(ret.IsBizSuccess){
                            $(".Src").val(ret.FileName);
                        }else{
                            layer.msg(ret.ErrMsg);
                            return false;
                        }
                    }
                })
                return false;
            }
        },
        seterrorMessage:function(data,str){
            if(data.length < 1 || data == ''){
                $(".pass-msg.error").html(str);
                return false;
            }else{
                $(".pass-msg.error").html('');
                return true;
            }
        }
    }
    add._init();
})